package com.SouthernWall_404.AgeRight.Common.API.LockAPI;

import com.SouthernWall_404.AgeRight.Common.AgeRight;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;

import java.util.UUID;

public class KeyInfo {

    public static String KEYPACK_TAG="keyPack";
    public static String BLOCK_TAG="block";
    public static String UUID_TAG="uuid";
    public static String X_TAG="x";
    public static String Y_TAG="y";
    public static String Z_TAG="z";


    public BlockPos blockPos;
    public String block_name;
    public UUID uuid;

    public KeyInfo(BlockPos blockPos, String block, UUID uuid) {
        this.blockPos = blockPos;
        this.block_name = block;
        this.uuid = uuid;
    }

    public KeyInfo(int x, int y, int z, String block, UUID uuid)
    {
        this(new BlockPos(x,y,z),block,uuid);
    }

    public KeyInfo()
    {
        this.blockPos =new BlockPos(0,0,0);
        this.block_name =" ";
        this.uuid = UUID.randomUUID();
    }

    public static KeyInfo getFromItem(ItemStack itemStack)
    {
        CompoundTag nbt=itemStack.getOrCreateTag();
        KeyInfo storeKeyInfo=new KeyInfo();
        if(!nbt.isEmpty())
        {
            CompoundTag modPack=(CompoundTag) nbt.get(AgeRight.MODID);
            storeKeyInfo=getFromModPack(modPack);
        }
        return storeKeyInfo;
    }

    public static KeyInfo getFromModPack(CompoundTag modPack)
    {
        KeyInfo storeKeyInfo=new KeyInfo();
        if(modPack.contains(KEYPACK_TAG))
        {
            CompoundTag keyPack=(CompoundTag) modPack.get(KEYPACK_TAG);
            storeKeyInfo=getFromKeyPack(keyPack);
        }

        return storeKeyInfo;
    }
    public static KeyInfo getFromKeyPack(CompoundTag keyPack)
    {
        KeyInfo storeKeyInfo=new KeyInfo();
        if(!keyPack.isEmpty())
        {
            int x=keyPack.getInt(X_TAG);
            int y=keyPack.getInt(Y_TAG);
            int z=keyPack.getInt(Z_TAG);
            String block=keyPack.getString(BLOCK_TAG);

            UUID uuid=keyPack.getUUID(UUID_TAG);

            storeKeyInfo=new KeyInfo(x,y,z,block,uuid);
        }

        return storeKeyInfo;
    }

    public CompoundTag getCompoundPack()
    {
        CompoundTag keyPack=new CompoundTag();

        keyPack.putInt(X_TAG,this.blockPos.getX());
        keyPack.putInt(Y_TAG,this.blockPos.getY());
        keyPack.putInt(Z_TAG,this.blockPos.getZ());
        keyPack.putUUID(UUID_TAG,uuid);
        keyPack.putString(BLOCK_TAG,block_name);


        return keyPack;
    }
}
